﻿// --------------------------------
// <copyright file="Product.cs" company="TRONSoft">
//     Copyright (c) TRONSoft.
// </copyright>
// <license>
//   This source code is subject to terms and conditions of the Microsoft
//   Public License. A copy of the license can be found in the License.html
//   file at the root of this distribution. If you cannot locate the
//   Microsoft Public License, please send an email to dlr@microsoft.com.
//   By using this source code in any fashion, you are agreeing to be bound 
//   by the terms of the Microsoft Public License. You must not remove this
//   notice, or any other, from this software.
// </license>
// <author>Tron</author>
// ---------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Tronsoft.Actucal.CalculationEngineCreator;

namespace Actucal
{
    [Serializable]
    public class Product
    {
        private string name;
        private readonly Dictionary<string, string> attributes = new Dictionary<string, string>();
        private readonly ConstantCollection constants = new ConstantCollection();

        public Product()
        {

        }

        public Product(string name)
        {
            this.Name = name;    
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public Dictionary<string, string> Attributes
        {
            get { return attributes; }
        }

        public void AddAttribute(string name, string value)
        {
            if (name == null) throw new ArgumentNullException("name");

            if (value == null)
            {
                value = string.Empty;
            }

            Attributes.Add(name, value);
        }

        public void DeleteAttribute(string name)
        {
            if (name == null) throw new ArgumentNullException("name");

            Attributes.Remove(name);
        }

        public string FindAttribute(string name)
        {
            if (name == null) throw new ArgumentNullException("name");

            if (!Attributes.ContainsKey(name))
            {
                throw new ArgumentException(name + "was not found."); 
            }

            return Attributes[name];
        }

        public bool ExistsAttribute(string name)
        {
            if (name == null) throw new ArgumentNullException("name");

            return Attributes.ContainsKey(name);
        }

        public ConstantCollection Constants
        {
            get { return constants; }
        }

        public CalculationParameterCollection GetParameters()
        {
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<string, string> pair in attributes)
            {
                sb.AppendLine(pair.Value);
            }

            //CalculationParameterCollection params =
            //new CalculationParameterCollection();

            CalculationParameterCollection parameters = new CalculationParameterCollection();
            foreach (Match match in Regex.Matches(sb.ToString(), @"\${[a-zA-Z]\w*?}", RegexOptions.Singleline | RegexOptions.IgnoreCase))
            {
                string parameterName = match.Value.Replace("${", string.Empty).Replace("}", string.Empty);
                parameters.Add(new CalculationParameter<int>(parameterName));
            }

            return parameters;
        }
    }
}
